<?php
	mysql_connect("localhost", "root", "%Tg34fv1")or die("cannot connect"); 
	mysql_select_db("wordWaffle")or die("cannot select DB");
	$myKey = mysql_real_escape_string(stripslashes($_GET['sesskey']));
	$gameID = mysql_real_escape_string(stripslashes($_GET['gameID']));	
	//validate sesskey
	$count=mysql_num_rows(mysql_query("SELECT * FROM players WHERE sesskey=$myKey"));
	if ($count==0) //unrecognized session key!
	{
		echo "Intruder alert!";
		return;
	}
	
	//check to see if game is over
	$result=mysql_query("SELECT * FROM gameStatus WHERE gameID=$gameID");
	if ($row=mysql_fetch_assoc($result))
	{
		if ($row['status']==3) //game finished successfully
		{
			echo 3;
			return;
		}
		if ($row['status']==4) //game finished unsuccessfully
		{
			echo 4;
			return;
		}
	}
	else
	{
		echo "GameID doesn't exist.";
		return;
	}
	//update my lastSeen timestamp
	$timestamp = date("Y-m-d H:i:s");
	mysql_query("UPDATE players SET lastSeen='$timestamp' WHERE sesskey=$myKey");
	
	//kick out anybody who hasn't been seen in 20 sec
	$twentySecsAgo = date_create();
	$twentySecsAgo = date("Y-m-d H:i:s",date_timestamp_get($twentySecsAgo) - 20);
	$lostPlayers = mysql_query("DELETE FROM players WHERE lastSeen < '$twentySecsAgo'");

	$playerCount = mysql_num_rows(mysql_query("SELECT * FROM players WHERE readyToPlay = 2"));
	
	//have I placed the most recent letter?
	$result=mysql_query("SELECT * FROM lettersGuessed WHERE letter IS NOT NULL ORDER BY turnNumber DESC");
	$lettersGuessed=mysql_num_rows($result);
	if ($lettersGuessed>0 && $playerCount>0)
	{
		$row = mysql_fetch_assoc($result);
		$mostRecentTurn = $row['turnNumber'];
		$mostRecentLetter = $row['letter'];
		$mostRecentChooserSess = $row['sesskey'];
		if (mysql_num_rows(mysql_query("SELECT * FROM waffle WHERE turnNumber=$mostRecentTurn AND sesskey=$myKey"))==0)
		{	//need to place letter
			$row = mysql_fetch_assoc(mysql_query("SELECT * FROM players WHERE sesskey=$mostRecentChooserSess"));
			$mostRecentChooser = $row['playerName'];
			echo "$mostRecentChooser has chosen the letter $mostRecentLetter.  Select a square for it.";
			return;
		}
		else 
		{	//waiting on anyone else?
			$waffleCount = mysql_num_rows(mysql_query("SELECT * FROM waffle"));
			if (($lettersGuessed * $playerCount) > $waffleCount)
			{
				echo "Waiting for other players to place $mostRecentLetter.";
				return;
			}
		}
	}
	
	//whose turn is it to choose?
	$result=mysql_query("SELECT * FROM lettersGuessed WHERE letter IS NULL ORDER BY turnNumber");
	if ($row = mysql_fetch_assoc($result))
	{
		$turnKey = $row['sesskey'];
		if ($turnKey == $myKey) //my turn
		{
			echo 1;
			return;
		}
		else
		{
			$result=mysql_query("SELECT * FROM players WHERE sesskey='$turnKey'");
			if (mysql_num_rows($result)==0)
			{
				//player not found.  kill game
				mysql_query("UPDATE players SET readyToPlay=0");
				mysql_query("UPDATE gameStatus SET status=4,endTime='$timestamp' WHERE gameID=$gameID");
				echo 2;
				return;
			}
			$row=mysql_fetch_assoc($result);
			$chooser=$row['playerName'];
			echo "$chooser's turn to choose a letter.";
			return;
		}
		
	}	
?>